package com.example.websocketrabbitmq.test;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 生产者
 */
public class Producer {
//        private final static String QUEUE_NAME = "test_queue";
    private final static String QUEUE_NAME = "printer";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.1.20");
        factory.setUsername("bailing");
        factory.setPassword("bailing123");
//        factory.setHost("192.168.1.83");
//        factory.setUsername("admin");
//        factory.setPassword("admin123");

        try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            //模拟打印机温度变化
            /*String message = "{\n" +
                    "  \"id\": 1890326408400584705,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_status_update\",\n" +
                    "    \"params\": [\n" +
                    "      {\n" +
                    "        \"extruder\": {\n" +
                    "          \"temperature\": 221.6\n" +
                    "        },\n" +
                    "        \"extruder1\": {\n" +
                    "          \"temperature\": 35.21\n" +
                    "        },\n" +
                    "        \"heater_bed\": {\n" +
                    "          \"temperature\": 45.07,\n" +
                    "          \"power\": 0.18676018616526924\n" +
                    "        }\n" +
                    "      },\n" +
                    "      167162.284665092\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}";*/
            //模拟打印机状态变化
            /*String message = "{\n" +
                    "  \"id\": 1890326408400584705,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_status_update\",\n" +
                    "    \"params\": [\n" +
                    "      {\n" +
                    "        \"print_stats\": {\n" +
                    "          \"state\": \"standby\"\n" +
                    "        }\n" +
                    "      },\n" +
                    "      167415.364561671\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}";*/
            //模拟打印机紧急停止
           /* String message = "{\n" +
                    "  \"id\": 1890326408400584705,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_klippy_shutdown\"\n" +
                    "  }\n" +
                    "}";*/
            //模拟打印机断开连接
            /*String message = "{\n" +
                    "  \"id\": 1890326408400584705,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_klippy_disconnected\"\n" +
                    "  }\n" +
                    "}";*/
            //模拟打印机连接成功
            /*String message = "{\n" +
                    "  \"id\": 1890326408400584705,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_klippy_ready\"\n" +
                    "  }\n" +
                    "}";*/
            //模拟打印机开始打印
            /*String message = "{\n" +
                    "  \"id\": 1890326408400584705,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_history_changed\",\n" +
                    "    \"params\": [\n" +
                    "      {\n" +
                    "        \"action\": \"added\",\n" +
                    "        \"job\": {\n" +
                    "          \"end_time\": null,\n" +
                    "          \"filament_used\": 0,\n" +
                    "          \"filename\": \"王-10件-470g-1d9h.gcode\",\n" +
                    "          \"metadata\": {\n" +
                    "            \"size\": 69963474,\n" +
                    "            \"modified\": 1739866911.6864498,\n" +
                    "            \"uuid\": \"b76811b9-f425-4f28-b7f3-b92f723b99e3\",\n" +
                    "            \"slicer\": \"OrcaSlicer\",\n" +
                    "            \"slicer_version\": \"2.2.0\",\n" +
                    "            \"gcode_start_byte\": 17586,\n" +
                    "            \"gcode_end_byte\": 69945891,\n" +
                    "            \"layer_count\": 1558,\n" +
                    "            \"object_height\": 311.6,\n" +
                    "            \"estimated_time\": 118973,\n" +
                    "            \"nozzle_diameter\": 0.4,\n" +
                    "            \"layer_height\": 0.2,\n" +
                    "            \"first_layer_height\": 0.2,\n" +
                    "            \"first_layer_extr_temp\": 260,\n" +
                    "            \"first_layer_bed_temp\": 95,\n" +
                    "            \"chamber_temp\": 70,\n" +
                    "            \"filament_name\": \"My Generic ABS - 标准\\\";\\\"My Generic ABS\",\n" +
                    "            \"filament_type\": \"ABS;ABS\",\n" +
                    "            \"filament_total\": 188140.93,\n" +
                    "            \"filament_weight_total\": 470.63,\n" +
                    "            \"thumbnails\": [\n" +
                    "              {\n" +
                    "                \"width\": 32,\n" +
                    "                \"height\": 32,\n" +
                    "                \"size\": 1018,\n" +
                    "                \"relative_path\": \".thumbs/王-10件-470g-1d9h-32x32.png\"\n" +
                    "              },\n" +
                    "              {\n" +
                    "                \"width\": 48,\n" +
                    "                \"height\": 48,\n" +
                    "                \"size\": 1079,\n" +
                    "                \"relative_path\": \".thumbs/王-10件-470g-1d9h-48x48.png\"\n" +
                    "              },\n" +
                    "              {\n" +
                    "                \"width\": 300,\n" +
                    "                \"height\": 300,\n" +
                    "                \"size\": 10309,\n" +
                    "                \"relative_path\": \".thumbs/王-10件-470g-1d9h-300x300.png\"\n" +
                    "              }\n" +
                    "            ]\n" +
                    "          },\n" +
                    "          \"print_duration\": 0,\n" +
                    "          \"status\": \"in_progress\",\n" +
                    "          \"start_time\": 1740034874.7888856,\n" +
                    "          \"total_duration\": 0.20684591599274427,\n" +
                    "          \"auxiliary_data\": [],\n" +
                    "          \"user\": \"_TRUSTED_USER_\",\n" +
                    "          \"exists\": true,\n" +
                    "          \"job_id\": \"000050\"\n" +
                    "        }\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}";*/
            //模拟打印机完成、取消打印
            /*String message = "{\n" +
                    "  \"id\": 1890326408400584705,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_history_changed\",\n" +
                    "    \"params\": [\n" +
                    "      {\n" +
                    "        \"action\": \"finished\",\n" +
                    "        \"job\": {\n" +
                    "          \"end_time\": 1740034986.1233215,\n" +
                    "          \"filament_used\": -10,\n" +
                    "          \"filename\": \"王-10件-470g-1d9h.gcode\",\n" +
                    "          \"metadata\": {\n" +
                    "            \"size\": 69963474,\n" +
                    "            \"modified\": 1739866911.6864498,\n" +
                    "            \"uuid\": \"b76811b9-f425-4f28-b7f3-b92f723b99e3\",\n" +
                    "            \"slicer\": \"OrcaSlicer\",\n" +
                    "            \"slicer_version\": \"2.2.0\",\n" +
                    "            \"gcode_start_byte\": 17586,\n" +
                    "            \"gcode_end_byte\": 69945891,\n" +
                    "            \"layer_count\": 1558,\n" +
                    "            \"object_height\": 311.6,\n" +
                    "            \"estimated_time\": 118973,\n" +
                    "            \"nozzle_diameter\": 0.4,\n" +
                    "            \"layer_height\": 0.2,\n" +
                    "            \"first_layer_height\": 0.2,\n" +
                    "            \"first_layer_extr_temp\": 260,\n" +
                    "            \"first_layer_bed_temp\": 95,\n" +
                    "            \"chamber_temp\": 70,\n" +
                    "            \"filament_name\": \"My Generic ABS - 标准\\\";\\\"My Generic ABS\",\n" +
                    "            \"filament_type\": \"ABS;ABS\",\n" +
                    "            \"filament_total\": 188140.93,\n" +
                    "            \"filament_weight_total\": 470.63,\n" +
                    "            \"thumbnails\": [\n" +
                    "              {\n" +
                    "                \"width\": 32,\n" +
                    "                \"height\": 32,\n" +
                    "                \"size\": 1018,\n" +
                    "                \"relative_path\": \".thumbs/王-10件-470g-1d9h-32x32.png\"\n" +
                    "              },\n" +
                    "              {\n" +
                    "                \"width\": 48,\n" +
                    "                \"height\": 48,\n" +
                    "                \"size\": 1079,\n" +
                    "                \"relative_path\": \".thumbs/王-10件-470g-1d9h-48x48.png\"\n" +
                    "              },\n" +
                    "              {\n" +
                    "                \"width\": 300,\n" +
                    "                \"height\": 300,\n" +
                    "                \"size\": 10309,\n" +
                    "                \"relative_path\": \".thumbs/王-10件-470g-1d9h-300x300.png\"\n" +
                    "              }\n" +
                    "            ]\n" +
                    "          },\n" +
                    "          \"print_duration\": 0,\n" +
                    "          \"status\": \"cancelled\",\n" +
                    "          \"start_time\": 1740034874.7888856,\n" +
                    "          \"total_duration\": 111.29192005301593,\n" +
                    "          \"auxiliary_data\": [],\n" +
                    "          \"user\": \"_TRUSTED_USER_\",\n" +
                    "          \"exists\": true,\n" +
                    "          \"job_id\": \"000050\"\n" +
                    "        }\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}";*/
            //模拟打印机打印中的任务时长
            /*String message = "{\n" +
                    "  \"id\": 1890326408400584705,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_status_update\",\n" +
                    "    \"params\": [\n" +
                    "      {\n" +
                    "        \"print_stats\": {\n" +
                    "          \"total_duration\": 1583.918108338985,\n" +
                    "          \"print_duration\": 1003.5531541039818\n" +
                    "        },\n" +
                    "        \"idle timeout\": {\n" +
                    "          \"printing time\": 978.4671443419938\n" +
                    "        },\n" +
                    "        \"motion _report\": {\n" +
                    "          \"live_position\": [\n" +
                    "            281.1612659475338,\n" +
                    "            279.4937340524662,\n" +
                    "            0.4,\n" +
                    "            1781.6402855294284\n" +
                    "          ]\n" +
                    "        },\n" +
                    "        \"toolhead\": {\n" +
                    "          \"estimated_print_time\": 164879.47418014443\n" +
                    "        },\n" +
                    "        \"extruder\": {\n" +
                    "          \"temperature\": 219.53,\n" +
                    "          \"power\": 0.736543158131223\n" +
                    "        }\n" +
                    "      },\n" +
                    "      164875.587082169\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}";*/
            /*String message= "{\n" +
                    "  \"id\": 1894997476205703200,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_status_update\",\n" +
                    "    \"params\": [\n" +
                    "      {\n" +
                    "        \"print_stats\": {\n" +
                    "          \"state\": \"null\",\n" +
                    "          \"total_duration\": 1583.918108338985,\n" +
                    "          \"print_duration\": 1003.5531541039818\n" +
                    "        },\n" +
                    "        \"extruder\": {\n" +
                    "          \"temperature\": 121.6\n" +
                    "        },\n" +
                    "        \"extruder1\": {\n" +
                    "          \"temperature\": 30.21\n" +
                    "        },\n" +
                    "        \"heater_bed\": {\n" +
                    "          \"temperature\": 45.07,\n" +
                    "          \"power\": 0.18676018616526924\n" +
                    "        }\n" +
                    "      },\n" +
                    "      167415.364561671\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}";*/
           /* String message = "{\n" +
                    "  \"id\": 1900006732127121400,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"notify_status_update\",\n" +
                    "    \"params\": [\n" +
                    "      {\n" +
                    "        \"print_stats\": {\n" +
                    "          \"total_duration\": 0.20894591696560383,\n" +
                    "          \"state\": \"printing\",\n" +
                    "          \"message\": \"\"\n" +
                    "        },\n" +
                    "        \"extruder1\": {\n" +
                    "          \"temperature\": 33.57\n" +
                    "        },\n" +
                    "        \"heater_bed\": {\n" +
                    "          \"temperature\": 48.76,\n" +
                    "          \"target\": 60,\n" +
                    "          \"power\": 1\n" +
                    "        },\n" +
                    "        \"virtual_sdcard\": {\n" +
                    "          \"file_path\": \"/home/klipper/printer_data/gcodes/冰沙机15-7H-180g.gcode\",\n" +
                    "          \"progress\": 0.0009290247992897653,\n" +
                    "          \"is_active\": true,\n" +
                    "          \"file_position\": 34181,\n" +
                    "          \"file_size\": 36792344\n" +
                    "        },\n" +
                    "        \"extruder\": {\n" +
                    "          \"temperature\": 120.98,\n" +
                    "          \"target\": 210,\n" +
                    "          \"power\": 1\n" +
                    "        }\n" +
                    "      },\n" +
                    "      1299324.75642345\n" +
                    "    ]\n"
                    "  }\n" +
                    "}";*/
            String message = "{\n" +
                    "  \"id\": 1900016483363807200,\n" +
                    "  \"data\": {\n" +
                    "    \"jsonrpc\": \"2.0\",\n" +
                    "    \"method\": \"printer_disconnected\"\n" +
                    "  }\n" +
                    "}";
            /*String message = "{\n" +
                    "  \"id\": 1890326408400584700,\n" +
                    "  \"data\": {\n" +
                    "    \"temperature\": {\n" +
                    "      \"tool0\": {\n" +
                    "        \"actual\": 214.8821,\n" +
                    "        \"target\": 220,\n" +
                    "        \"offset\": 0\n" +
                    "      },\n" +
                    "      \"tool1\": {\n" +
                    "        \"actual\": 25.3,\n" +
                    "        \"target\": null,\n" +
                    "        \"offset\": 0\n" +
                    "      },\n" +
                    "      \"bed\": {\n" +
                    "        \"actual\": 50.221,\n" +
                    "        \"target\": 70,\n" +
                    "        \"offset\": 5\n" +
                    "      },\n" +
                    "      \"history\": [\n" +
                    "        {\n" +
                    "          \"time\": 1395651928,\n" +
                    "          \"tool0\": {\n" +
                    "            \"actual\": 214.8821,\n" +
                    "            \"target\": 220\n" +
                    "          },\n" +
                    "          \"tool1\": {\n" +
                    "            \"actual\": 25.3,\n" +
                    "            \"target\": null\n" +
                    "          },\n" +
                    "          \"bed\": {\n" +
                    "            \"actual\": 50.221,\n" +
                    "            \"target\": 70\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"time\": 1395651926,\n" +
                    "          \"tool0\": {\n" +
                    "            \"actual\": 212.32,\n" +
                    "            \"target\": 220\n" +
                    "          },\n" +
                    "          \"tool1\": {\n" +
                    "            \"actual\": 25.1,\n" +
                    "            \"target\": null\n" +
                    "          },\n" +
                    "          \"bed\": {\n" +
                    "            \"actual\": 49.1123,\n" +
                    "            \"target\": 70\n" +
                    "          }\n" +
                    "        }\n" +
                    "      ]\n" +
                    "    },\n" +
                    "    \"sd\": {\n" +
                    "      \"ready\": true\n" +
                    "    },\n" +
                    "    \"state\": {\n" +
                    "      \"text\": \"Operational\",\n" +
                    "      \"flags\": {\n" +
                    "        \"operational\": true,\n" +
                    "        \"paused\": false,\n" +
                    "        \"printing\": false,\n" +
                    "        \"cancelling\": false,\n" +
                    "        \"pausing\": false,\n" +
                    "        \"sdReady\": true,\n" +
                    "        \"error\": false,\n" +
                    "        \"ready\": true,\n" +
                    "        \"closedOrError\": false\n" +
                    "      }\n" +
                    "    }\n" +
                    "  }\n" +
                    "}";*/
           /* String message = "{\n" +
                    "  \"id\": 1890326408400584700,\n" +
                    "  \"data\": {\n" +
                    "    \"job\": {\n" +
                    "      \"file\": {\n" +
                    "        \"name\": \"whistle_v2.gcode\",\n" +
                    "        \"origin\": \"local\",\n" +
                    "        \"size\": 1468987,\n" +
                    "        \"date\": 1378847754\n" +
                    "      },\n" +
                    "      \"estimatedPrintTime\": 8811,\n" +
                    "      \"filament\": {\n" +
                    "        \"tool0\": {\n" +
                    "          \"length\": 810,\n" +
                    "          \"volume\": 5.36\n" +
                    "        }\n" +
                    "      }\n" +
                    "    },\n" +
                    "    \"progress\": {\n" +
                    "      \"completion\": 0.2298468264184775,\n" +
                    "      \"filepos\": 337942,\n" +
                    "      \"printTime\": 276,\n" +
                    "      \"printTimeLeft\": 912\n" +
                    "    },\n" +
                    "    \"state\": \"Printing\"\n" +
                    "  }\n" +
                    "}";*/
            //队列和消息均持久化
            AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
                    .deliveryMode(2)
                    .build();
            channel.basicPublish("", QUEUE_NAME, props, message.getBytes());
            System.out.println("消息已发送");
        }
    }
}
